package mysql

import (
	"gorm.io/gorm"
	"video-system/userrpc/userrpc"
)

type CollectionInfo struct {
	gorm.Model
	UserId        int64 `gorm:"type:int(11);not null;comment:'用户id'"`
	VideoId       int64 `gorm:"type:int(11);not null;comment:'视频id'"`
	CollectStatus int64 `gorm:"type:int(11);not null;default:0;comment:'收藏状态'"`
}

func (i *CollectionInfo) FindCollectId(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("collection_infos.user_id = ?", id).Where("collection_infos.video_id", id2).Find(i).Error
}

func (i *CollectionInfo) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(i).Error
}

func (i *CollectionInfo) CollectDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("collection_infos.id = ?", id).Delete(i).Error
}

func CollectionList(db *gorm.DB, id int64) []*userrpc.CollectList {
	var list []*userrpc.CollectList

	db.Debug().Model(&CollectionInfo{}).Select("video_infos.*,user_infos.nick_name,user_infos.avatar_link").
		Joins("left join video_infos on video_infos.id = collection_infos.video_id").
		Joins("left join user_infos on user_infos.id = video_infos.upload_user_id").
		Where("collection_infos.user_id = ?", id).
		Find(&list)
	return list
}
